<?php

class VES_VendorsWarehouse_Block_Address extends Mage_Adminhtml_Block_Widget_Form
{
    protected $_formId="address_save_form";
    protected function _prepareLayout()
        {
            $this->setChild('form_after',
                $this->getLayout()->createBlock('vendorswarehouse/form_after',
                    'vendorswarehouse.form.after')
                );
            return parent::_prepareLayout();
         }
    
	protected function _prepareForm()
	{
		$form = new Varien_Data_Form(
		  array(
		    'id' => $this->_formId,
		    'action' => $this->getUrl('*/warehouse_address/savepost', array('id' => $this->getRequest()->getParam('id'))),
		    'method' => 'post',
		    )
		    );
		$this->setForm($form);
		$form->setUseContainer(true);
		
		$fieldset = $form->addFieldset('vendors_form', array('legend'=>Mage::helper('vendors')->__('Vendor warehouse information')));
	      
		$fieldset->addField('sender_zip_code', 'label', array(
		    'label'     => Mage::helper('vendors')->__('仓库编号'),
		    'class'     => 'required-entry',
		    //  'required'  => true,
		    'name'      => 'sender_zip_code',
		));
		
		$fieldset->addField('sender_name', 'text', array(
		  'label'     => Mage::helper('vendors')->__('仓库名称'),
		  'class'     => 'required-entry',
	      'required'  => true,
		  'name'      => 'sender_name',
		));
		$fieldset->addField('sender_address', 'text', array(
		    'label'     => Mage::helper('vendors')->__('仓库地址'),
		    'class'     => 'required-entry',
		      'required'  => true,
		    'name'      => 'sender_address',
		));
		$fieldset->addField('sender_contact', Mage::app()->getStore()->isAdmin()?'text':'text', array(
		    'label'     => Mage::helper('vendors')->__('负责人名称'),
		     'class'     => '',
		     'required'  => true,
		    'name'      => 'sender_contact',
		));
		$fieldset->addField('sender_phone', 'text', array(
		  'label'     => Mage::helper('vendors')->__('负责人电话'),
		  'class'     => 'validate-number',
		  'required'  => true,
		  'name'      => 'sender_phone',
		));
		$fieldset->addField('sender_email', Mage::app()->getStore()->isAdmin()?'text':'text', array(
		  'label'     => Mage::helper('vendors')->__('负责人邮箱'),
		  'class'     => 'validate-email',
		  'name'      => 'sender_email',
		));
		
		$fieldset->addField('status','select', array(
		    'label'     => Mage::helper('vendors')->__('状态'),
		    'class'   => 'required-entry validate-select',
		    'name'      => 'status',
		    'values'      =>Mage::getModel('vendorswarehouse/address')->getAvailableStatusOption()
		));
		
		$countryOptions = Mage::getModel('adminhtml/system_config_source_country')->toOptionArray();
		$cOptions = array();
		foreach($countryOptions as $option){
		    $cOptions[$option['value']] = $option['label'];
		}
		$fieldset->addField('sender_country', 'select', array(
		    'label'     => Mage::helper('vendorswarehouse')->__('国家'),
		    'name'      => 'sender_country',
		    'class'   => 'validate-select',
		    'options'	  => $cOptions,
		));
		

		$fieldset->addField('sender_province_id', 'text', array(
		    'label'     => Mage::helper('vendorswarehouse')->__('省份'),
		    'required'  => false,
		    'name'      => 'sender_province_id',
		));
		
		$fieldset->addField('sender_province', 'select', array(
		    'label'     => Mage::helper('vendorswarehouse')->__('省份'),
		    'class'   => 'validate-select',
		    'required'  => true,
		    'name'      => 'sender_province',
		));
		
		$regionElement = $form->getElement('sender_province');
		$regionElement->setRequired(true);
		if ($regionElement) {
			$regionElement->setRenderer(Mage::getModel('vendors/vendor_renderer_province'));
		}
	
		$regionElement = $form->getElement('sender_province_id');
		if ($regionElement) {
			$regionElement->setNoDisplay(true);
		}
		$fieldset->addField('sender_city', 'text', array(
		    'label'     => Mage::helper('vendorswarehouse')->__('城市'),
		    'class'     => 'required-entry',
		    'required'  => true,
		    'name'      => 'sender_city',
		));
        //根据id来自动填写表单值
		if(Mage::registry("warehouse_detailsData")){
		    $datas = Mage::registry('warehouse_detailsData')->getData();
		    $data = $datas[0];
		    $form->setValues($data);
		}
		$fieldset->addField('save_button', 'note', array(
		    'text' => $this->getButtonHtml(
		        Mage::helper('catalog')->__('Save'),
		        'addressForm.submit()',
		        'save'
		        )
		));
	
		return parent::_prepareForm();
	}
	/**
	 * Retrieve predefined additional element types
	 *
	 * @return array
	 */
	protected function _getAdditionalElementTypes()
	{
	    return array('ves_file'=>'VES_Vendors_Block_Form_Element_File');
	}
	
	/**
     * Set Fieldset to Form
     *
     * @param array $attributes attributes that are to be added
     * @param Varien_Data_Form_Element_Fieldset $fieldset
     * @param array $exclude attributes that should be skipped
     */
    protected function _setFieldset($attributes, $fieldset, $exclude=array())
    {
        $this->_addElementTypes($fieldset);
        foreach ($attributes as $attribute) {
            /* @var $attribute Mage_Eav_Model_Entity_Attribute */
            if (!$attribute || ($attribute->hasIsVisible() && !$attribute->getIsVisible())) {
                continue;
            }
            if ( ($inputType = $attribute->getFrontend()->getInputType())
                 && !in_array($attribute->getAttributeCode(), $exclude)
                 && ('media_image' != $inputType)
                 ) {

                $fieldType      = $inputType;
                if($fieldType == 'boolean'){
					$fieldType = 'select';
                }elseif($fieldType == 'file'){
                    $fieldType = 'ves_file';
                }
                
                $rendererClass  = $attribute->getFrontend()->getInputRendererClass();
                if (!empty($rendererClass)) {
                    $fieldType  = $inputType . '_' . $attribute->getAttributeCode();
                    $fieldset->addType($fieldType, $rendererClass);
                }

                $element = $fieldset->addField($attribute->getAttributeCode(), $fieldType,
                    array(
                        'name'      => $attribute->getAttributeCode(),
                        'label'     => $attribute->getFrontend()->getLabel(),
                        'class'     => $attribute->getFrontend()->getClass(),
                        'required'  => $attribute->getIsRequired(),
                        'note'      => $attribute->getNote(),
                    )
                )
                ->setEntityAttribute($attribute);

                $element->setAfterElementHtml($this->_getAdditionalElementHtml($element));

                if ($inputType == 'select') {
                    $element->setValues($attribute->getSource()->getAllOptions(true, true));
                } else if ($inputType == 'multiselect') {
                    $element->setValues($attribute->getSource()->getAllOptions(false, true));
                    $element->setCanBeEmpty(true);
                } else if ($inputType == 'date') {
                    $element->setImage($this->getSkinUrl('images/grid-cal.gif'));
                    $element->setFormat(Mage::app()->getLocale()->getDateFormatWithLongYear());
                } else if ($inputType == 'datetime') {
                    $element->setImage($this->getSkinUrl('images/grid-cal.gif'));
                    $element->setTime(true);
                    $element->setStyle('width:50%;');
                    $element->setFormat(
                        Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
                    );
                } else if ($inputType == 'multiline') {
                    $element->setLineCount($attribute->getMultilineCount());
                }elseif($inputType=='boolean'){
                	$values = array(
						array('label' => Mage::helper('vendors')->__('No'),'value'=>0),
						array('label' => Mage::helper('vendors')->__('Yes'),'value'=>1),
					);
                	$element->setValues($values);
                }
            }
        }
    }
    
	/**
     * Get All additional attributes
     * 
     * @return Mage_Eav_Model_Resource_Entity_Attribute_Collection
     */
    public function getAdditionalAttributes(){
    	$vendorAttributeType = Mage::getResourceModel('eav/entity_type_collection')->addFieldToFilter('entity_type_code','ves_vendor')->getFirstItem();
    	$collection = Mage::getResourceModel('eav/entity_attribute_collection')
	      ->addFieldToFilter('entity_type_id',$vendorAttributeType->getId())
	      ->addFieldToFilter('is_user_defined',true);
	    return $collection;
    }
    public function getValidateUrl()
    {
        return $this->getUrl("*/warehouse_address/validate");
    }
    public function getFormId(){
        return $this->_formId;
    }
}